<template>
  <el-row :gutter="24" class="pl80">
    <el-col>
      <h2>
        责任制情况
        <el-button class="fr" type="primary" icon="el-icon-plus" @click="handleEdit(false, {})" />
      </h2>
    </el-col>
    <el-col>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="evalDate" label="报告或考核时间" align="center"></el-table-column>
        <el-table-column align="center" label="民主测评得票及比例">
          <el-table-column align="center" prop="goodPct" label="好">
            <template slot-scope="scope">{{ scope.row.goodPct ? scope.row.goodPct + '%' : null }}</template>
          </el-table-column>
          <el-table-column align="center" prop="littleGoodPct" label="较好">
            <template
              slot-scope="scope"
            >{{ scope.row.littleGoodPct ? scope.row.littleGoodPct + '%' : null }}</template>
          </el-table-column>
          <el-table-column align="center" prop="generalPct" label="一般">
            <template
              slot-scope="scope"
            >{{ scope.row.generalPct ? scope.row.generalPct + '%' : null }}</template>
          </el-table-column>
          <el-table-column align="center" prop="littleBadPct" label="较差">
            <template
              slot-scope="scope"
            >{{ scope.row.littleBadPct ? scope.row.littleBadPct + '%' : null }}</template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" prop="evalConclude" label="考核结论"></el-table-column>
        <el-table-column align="center" prop="citeCase" label="受表彰情况"></el-table-column>
        <el-table-column prop="dutyChargeCase" align="center" label="被责任追究情况"></el-table-column>
        <el-table-column prop="processUnit" align="center" label="表彰或追究单位"></el-table-column>
        <el-table-column prop="refNo" align="center" label="文号"></el-table-column>
        <el-table-column name="action" fixed="right" width="50" align="center">
          <template slot-scope="scope">
            <el-dropdown>
              <a class="el-dropdown-link">
                <i class="el-icon-menu"></i>
              </a>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                  <a>修改</a>
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(scope.row)">
                  <a class="red">删除</a>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <duty-modal ref="dutyModal" @refresh="handleRefresh" />
  </el-row>
</template>

<script>
import { getUserDuty, deleteUserDuty } from '@/api/archives'
import { openNotification, getSession, setSession } from '@/utils/util'
import DutyModal from './DutyModal'

export default {
  components: {
    DutyModal
  },
  data() {
    return {
      loading: false,
      tableData: [],
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      leaderRow: JSON.parse(this.$route.query.leaderRow)
    }
  },
  methods: {
    handleRefresh() {
      this.handleGetList(this.leaderRow.userId)
    },
    async handleGetList(id, param = {}) {
      this.loading = true
      const res = await getUserDuty(id, param)
      if (res && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    },
    handleEdit(bool, row) {
      this.$nextTick(() => {
        this.$refs.dutyModal.edit(bool, row)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteUserDuty(row.userDutyId, {})
          openNotification('success', '删除成功')
          _this.handleGetList(_this.leaderRow.userId)
        }
      })
    }
  },
  created() {
    if (this.leaderRow) {
      setSession('leaderRow', this.leaderRow)
    } else {
      this.leaderRow = getSession('leaderRow')
    }
    this.handleGetList(this.leaderRow.userId)
  }
}
</script>
